# -*- coding: utf-8 -*-
#!/usr/bin/env python

import os
import sys
import urllib2
import re
import hashlib

class KBSattDL():
	'''KBSattDL class: a class contain some functions which are useful for download att '''
	def __init__(self):
		'''BoardList define what board should be watched. keys are English Names, and values are board ids which are defined by system administrator of the BBS. So you should manully modify it as you will.'''
		self.BoardList = {"MyPhoto":874, "PieLove":398}
				
	def getHtml(self):
		'''Read the mainpage.html of BBS. Notice: frames.html has many javascript scripts to load the mainpage. To avoid resolving scripts, I go to the mainpage.html directly. Return the list of wonder board.'''
		dlList = []
		try:
			f = urllib2.urlopen("http://www.newsmth.net/mainpage.html")
			mainpage = f.read()
		except (urllib2.HTTPError, urllib2.URLError, IOError):
			sys.exit(1)
			
		list = re.findall('board=(\w+?)&gid=(\d+)', mainpage)

		self.dlList = list
		
	def download(self):
		'''Build the post URL and follow the URL to get the att URL. I use some regex. Notice: the KBS pages are encoded by GB2312, and this script is UTF-8. So I must decode the page and convert it to UTF-8 before the regex.'''
		BoardName = self.BoardList.keys()
		for (board, pid) in self.dlList:
			if board in BoardName:
				dlURL = "http://www.newsmth.net/bbscon.php?bid=" + str(self.BoardList[board]) + "&id=" + str(pid)
			else:
				continue
			try:
				post = urllib2.urlopen(dlURL).read()
			except (urllib2.HTTPError, urllib2.URLError, IOError):
				continue

			pattern = "发信人: (\w+)".decode('utf-8').encode('gb2312')
			poster = re.findall(pattern, post)[0]
			pattern = "信区: (\w+)".decode('utf-8').encode('gb2312')
			postBoard = re.findall(pattern, post)[0]
			
			atts = re.findall("attach\((.*?), (\d+), (\d+)\)", post)
			for att in atts:
				attURL = "http://att.newsmth.net/att.php?p."+str(self.BoardList[board])+"."+pid+"."+att[2]+".jpg"
				try:
					data = urllib2.urlopen(attURL).read()
					#How to store the att?
					filename = poster + "-" + postBoard + "-" + hashlib.md5(attURL).hexdigest() + ".jpg"
					f = open(filename, "w")
					f.write(data)
					f.close()
				except (urllib2.HTTPError, urllib2.URLError, IOError):
					continue
	def run(self):
		self.getHtml()
		self.download()
	

if __name__ == "__main__":
	
	instance = KBSattDL()
	instance.run()
